package com.ruoyi.project.modules.processBranch.controller;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.project.modules.interfaceinfo.domain.InterfaceInfo;
import com.ruoyi.project.modules.interfaceinfo.domain.VirtualUsage;
import com.ruoyi.project.modules.interfaceinfo.service.IInterfaceInfoService;
import com.ruoyi.project.modules.interfaceinfo.service.IVirtualUsageService;
import com.ruoyi.project.modules.interfaceinfo.vo.FieldSourceVo;
import com.ruoyi.project.modules.interfaceinfo.vo.InterfaceParam;
import com.ruoyi.project.modules.parameter.domain.InterfaceParamsFieldRelation;
import com.ruoyi.project.modules.parameter.domain.ParamsFieldRelation;
import com.ruoyi.project.modules.parameter.service.IInterfaceParamsFieldRelationService;
import com.ruoyi.project.modules.parameter.service.IParamsFieldRelationService;
import com.ruoyi.project.modules.processBranch.vo.ParamObject;
import com.ruoyi.project.modules.processBranch.vo.ProcessBranchVo;
import com.ruoyi.project.modules.processBranchCondition.domain.ProcessBranchCondition;
import com.ruoyi.project.modules.processBranchCondition.service.IProcessBranchConditionService;
import com.ruoyi.project.modules.processBranchConditionDetail.domain.ProcessBranchConditionDetail;
import com.ruoyi.project.modules.processBranchConditionDetail.service.IProcessBranchConditionDetailService;
import com.ruoyi.project.modules.processBranchEnd.domain.ProcessBranchEnd;
import com.ruoyi.project.modules.processBranchEnd.service.IProcessBranchEndService;
import com.ruoyi.project.modules.virtualUsageGroup.domain.VirtualUsageGroup;
import com.ruoyi.project.modules.virtualUsageGroup.service.IVirtualUsageGroupService;
import com.ruoyi.project.modules.virtualUsageParamSource.controller.VirtualUsageParamSourceController;
import com.ruoyi.project.modules.virtualUsageParamSource.domain.VirtualUsageParamSource;
import com.ruoyi.project.modules.virtualUsageParamSource.po.VirtualUsageParamSourcePo;
import com.ruoyi.project.modules.virtualUsageParamSource.service.IVirtualUsageParamSourceService;
import com.ruoyi.project.modules.virtualUsageParamSource.vo.InterfaceObjectParam;
import com.ruoyi.project.modules.virtualUsageParamSource.vo.ObjectParam;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.processBranch.domain.ProcessBranch;
import com.ruoyi.project.modules.processBranch.service.IProcessBranchService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.xml.transform.Result;

/**
 * 流程分支Controller
 *
 * @author smallrain
 * @date 2022-04-02
 */
@Controller
@RequestMapping("/processBranch/processBranch")
public class ProcessBranchController extends BaseController {
    private String prefix = "processBranch/processBranch";

    @Autowired
    private IProcessBranchService processBranchService;

    @RequiresPermissions("processBranch:processBranch:view")
    @GetMapping()
    public String processBranch() {
        return prefix + "/processBranch";
    }

    /**
     * 查询流程分支列表
     */
    @RequiresPermissions("processBranch:processBranch:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProcessBranch processBranch) {
        startPage();
        List<ProcessBranch> list = processBranchService.selectProcessBranchList(processBranch);
        return getDataTable(list);
    }

    /**
     * 导出流程分支列表
     */
    @RequiresPermissions("processBranch:processBranch:export")
    @Log(title = "流程分支", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProcessBranch processBranch) {
        List<ProcessBranch> list = processBranchService.selectProcessBranchList(processBranch);
        ExcelUtil<ProcessBranch> util = new ExcelUtil<ProcessBranch>(ProcessBranch.class);
        return util.exportExcel(list, "流程分支数据");
    }

    /**
     * 新增流程分支
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存流程分支
     */
    @RequiresPermissions("processBranch:processBranch:add")
    @Log(title = "流程分支", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProcessBranch processBranch) {
        return toAjax(processBranchService.insertProcessBranch(processBranch));
    }

    /**
     * 修改流程分支
     */
    @GetMapping("/edit/{processBranchId}")
    public String edit(@PathVariable("processBranchId") Long processBranchId, ModelMap mmap) {
        ProcessBranch processBranch = processBranchService.selectProcessBranchByProcessBranchId(processBranchId);
        mmap.put("processBranch", processBranch);
        return prefix + "/edit";
    }

    /**
     * 修改保存流程分支
     * eg:
     * processBranchId=1&aliasName=xxxx
     */
    //@RequiresPermissions("processBranch:processBranch:edit")
    @Log(title = "流程分支", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody ProcessBranch processBranch) {
        return toAjax(processBranchService.updateProcessBranch(processBranch));
    }

    /**
     * 删除流程分支
     */
    @RequiresPermissions("processBranch:processBranch:remove")
    @Log(title = "流程分支", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(processBranchService.deleteProcessBranchByProcessBranchIds(ids));
    }

    @Resource
    private IVirtualUsageService virtualUsageService;

    @Resource
    private IProcessBranchConditionService processBranchConditionService;

    @Resource
    private IProcessBranchEndService processBranchEndService;

    /**
     * 根据接口id， 获取流程分支编排
     * @author chenxiaoyu
     * @date 2022/4/3 上午1:53
     * @param interfaceId 接口id
     * @param conditionFlag 1-只看流程条件 2-全部
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/queryProcessBranch")
    @ResponseBody
    public AjaxResult queryProcessBranch(Long interfaceId, String conditionFlag) {
        return AjaxResult.success(query(interfaceId, conditionFlag));
    }

    private List<ProcessBranchVo> query(Long interfaceId, String conditionFlag) {
        ProcessBranch processBranch = new ProcessBranch();
        processBranch.setInterfaceId(interfaceId);
        List<ProcessBranch> processBranches = processBranchService.selectProcessBranchList(processBranch);
        if (CollectionUtil.isNotEmpty(processBranches)) {
            VirtualUsage virtualUsage = new VirtualUsage();
            virtualUsage.setRelationInterfaceId(interfaceId);
            List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsages(virtualUsage);
            Map<Long, List<VirtualUsage>> virtualUsageCollect = virtualUsages.stream().collect(Collectors.groupingBy(VirtualUsage::getId));

            ProcessBranchCondition processBranchCondition = new ProcessBranchCondition();
            processBranchCondition.setInterfaceId(interfaceId);
            List<ProcessBranchCondition> processBranchConditions = this.processBranchConditionService.selectProcessBranchConditionList(processBranchCondition);
            Map<Long, List<ProcessBranchCondition>> processBranchConditionCollect = processBranchConditions.stream().collect(Collectors.groupingBy(ProcessBranchCondition::getBranchConditionId));

            ProcessBranchEnd processBranchEnd = new ProcessBranchEnd();
            processBranchEnd.setInterfaceId(interfaceId);
            List<ProcessBranchEnd> processBranchEnds = this.processBranchEndService.selectProcessBranchEndList(processBranchEnd);
            Map<Long, List<ProcessBranchEnd>> processBranchEndsCollect = processBranchEnds.stream().collect(Collectors.groupingBy(ProcessBranchEnd::getBranchEndId));

            VirtualUsageGroup virtualUsageGroup = new VirtualUsageGroup();
            virtualUsageGroup.setInterfaceId(interfaceId);
            List<VirtualUsageGroup> virtualUsageGroups = this.virtualUsageGroupService.selectVirtualUsageGroupList(virtualUsageGroup);
            Map<Long, List<VirtualUsageGroup>> virtualUsageGroupsCollect = virtualUsageGroups.stream().collect(Collectors.groupingBy(VirtualUsageGroup::getId));

            for (ProcessBranch branch : processBranches) {
                Long entityId = branch.getEntityId();

                if (branch.getType().equals("1")) {
                    List<VirtualUsage> virtualUsages1 = virtualUsageCollect.get(entityId);
                    if (CollectionUtil.isNotEmpty(virtualUsages1)) {
                        if (StringUtils.isNotEmpty(branch.getAliasName())) {
                            branch.setProcessBranchName(branch.getAliasName());
                            branch.setProcessBranchDescription(branch.getAliasName());
                        } else {
                            branch.setProcessBranchName(virtualUsages1.get(0).getSlaveInterfaceName());
                            branch.setProcessBranchDescription(virtualUsages1.get(0).getSlaveInterfaceName());
                        }

                    }
                    branch.setColor("blue");
                } else if (branch.getType().equals("4")) {
                    List<VirtualUsageGroup> virtualUsagesGroup = virtualUsageGroupsCollect.get(entityId);
                    if (CollectionUtil.isNotEmpty(virtualUsagesGroup)) {
                        branch.setProcessBranchName(virtualUsagesGroup.get(0).getGroupName());
                        branch.setProcessBranchDescription(virtualUsagesGroup.get(0).getGroupName());
                    }
                    branch.setColor("purple");
                } else if (branch.getType().equals("2")) {
                    List<ProcessBranchCondition> processBranchConditions1 = processBranchConditionCollect.get(entityId);
                    if (CollectionUtil.isNotEmpty(processBranchConditions1)) {
                        branch.setProcessBranchName(processBranchConditions1.get(0).getBranchConditionName());
                        branch.setConditionType(processBranchConditions1.get(0).getType());
                        String branchConditionDesc = processBranchConditions1.get(0).getBranchConditionDesc();
                        if ("if".equals(processBranchConditions1.get(0).getType())) {
                            branch.setProcessBranchDescription(branchConditionDesc == null ? "" : branchConditionDesc.replace("如果", "if"));
                        } else if ("elseif".equals(processBranchConditions1.get(0).getType())) {
                            branch.setProcessBranchDescription(branchConditionDesc == null ? "" : branchConditionDesc.replace("如果", "elseif"));
                        } else {
                            branch.setProcessBranchDescription(branchConditionDesc == null ? "" : branchConditionDesc.replace("否则", "else"));
                        }
                    }
                    branch.setColor("yellow");

                } else if (branch.getType().equals("3")) {
                    branch.setProcessBranchName("结束");
                    List<ProcessBranchEnd> processBranchEnds1 = processBranchEndsCollect.get(entityId);
                    if (CollectionUtil.isNotEmpty(processBranchEnds1)) {
                        branch.setEndType(processBranchEnds1.get(0).getEndType());
                        branch.setEndWay(processBranchEnds1.get(0).getEndWay());
                        if ("1".equals(processBranchEnds1.get(0).getEndWay())) {
                            branch.setColor("green");
                            branch.setProcessBranchName("正常结束");
                            branch.setProcessBranchDescription("正常结束");
                        } else if ("2".equals(processBranchEnds1.get(0).getEndWay())) {
                            branch.setColor("red");
                            branch.setProcessBranchName("异常结束");
                            branch.setProcessBranchDescription("异常结束");
                        } else if ("3".equals(processBranchEnds1.get(0).getEndWay())) {
                            branch.setColor("yellow2");
                            branch.setProcessBranchName("跳出循环");
                            branch.setProcessBranchDescription("跳出循环");
                        } else {
                            branch.setColor("yellow3");
                            branch.setProcessBranchName("继续循环");
                            branch.setProcessBranchDescription("继续循环");
                        }
                    }
                }
            }
            return findTree(processBranches, conditionFlag);
        }
        return new ArrayList<>();
    }

    /**
     * 根据接口id， 获取流程分支编排故事
     * @author chenxiaoyu
     * @date 2022/4/3 上午1:53
     * @param interfaceId 接口id
     * @param conditionFlag 1-只看流程条件 2-全部
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/queryProcessBranchStory")
    @ResponseBody
    public AjaxResult queryProcessBranchStory(Long interfaceId, String conditionFlag) {
        List<ProcessBranchVo> processBranchVoList = query(interfaceId, conditionFlag);
        if (CollectionUtil.isNotEmpty(processBranchVoList)) {
            loop(processBranchVoList, conditionFlag);
        }
        return AjaxResult.success(processBranchVoList);
    }

    private void loop(List<ProcessBranchVo> processBranchVoList, String conditionFlag) {
        Iterator<ProcessBranchVo> iterator = processBranchVoList.iterator();
        ProcessBranchVo pre = null, ifBranch = null;
        List<ProcessBranchVo> elseIfBranchs = new ArrayList<>();
        while (iterator.hasNext()) {
            ProcessBranchVo processBranchVo = iterator.next();
            if ("1".equals(conditionFlag) && processBranchVo.getType().equals("3")) {
                iterator.remove();
                continue;
            }
            if (processBranchVo.getColor().equals("green")) {
                if (pre != null) {
                    pre.setPointColor("green");
                    pre.setProcessBranchDescription(pre.getProcessBranchDescription() + " 流程结束");
                }
                iterator.remove();
                continue;
            } else if (processBranchVo.getColor().equals("yellow")) {
                if ("if".equals(processBranchVo.getConditionType())) {
                    ifBranch = processBranchVo;
                    elseIfBranchs = new ArrayList<>();
                } else if ("elseif".equals(processBranchVo.getConditionType())) {
                    elseIfBranchs.add(processBranchVo);
                } else {
                    if (CollectionUtil.isNotEmpty(processBranchVo.getChildren())) {
                        if (processBranchVo.getChildren().size() == 1) {
                            if (processBranchVo.getChildren().get(0).getColor().equals("red")) {
                                if (ifBranch != null) {
                                    ifBranch.setPointColor("red");
                                    ifBranch.setProcessBranchDescription(ifBranch.getProcessBranchDescription() + "， 否则流程异常结束");
                                }
                                if (elseIfBranchs.size() > 0) {
                                    for (ProcessBranchVo elseIfBranch : elseIfBranchs) {
                                        elseIfBranch.setPointColor("red");
                                        elseIfBranch.setProcessBranchDescription(elseIfBranch.getProcessBranchDescription() + "， 否则流程异常结束");
                                    }
                                }
                                iterator.remove();
                                continue;
                            }
                        }
                    }

                }
            }
            pre = processBranchVo;
            if (CollectionUtil.isNotEmpty(processBranchVo.getChildren())) {
                loop(processBranchVo.getChildren(), conditionFlag);
            }
        }
    }

    public List<ProcessBranchVo> findTree(List<ProcessBranch> processBranches, String conditionFlag) {
        List<ProcessBranchVo> treeData = new ArrayList<>();
        int index = 1;
        for (ProcessBranch processBranch : processBranches) {
            if ("1".equals(conditionFlag)) {
                if (processBranch.getType().equals("1") || processBranch.getType().equals("3") || processBranch.getType().equals("4")) {
                    continue;
                }
            }
            if (processBranch.getParentProcessBranchId() == 0) {
                ProcessBranchVo processBranchVo = new ProcessBranchVo();
                BeanUtils.copyProperties(processBranch, processBranchVo);
                processBranchVo.setIndex(index + "");
                index ++;
                treeData.add(processBranchVo);
            }
        }
        findChildren(treeData, processBranches, conditionFlag);
        return treeData;
    }

    private void findChildren(List<ProcessBranchVo> treeData, List<ProcessBranch> processBranches, String conditionFlag) {
        for (ProcessBranchVo processBranch : treeData) {
            List<ProcessBranchVo> children = new ArrayList<>();
            int index = 1;
            for (ProcessBranch child : processBranches) {
                if (processBranch.getProcessBranchId().equals(child.getParentProcessBranchId())) {
                    if ("1".equals(conditionFlag)) {
                        if (child.getType().equals("1") || child.getType().equals("4")) {
                            continue;
                        }
                    }
                    ProcessBranchVo processBranchVo = new ProcessBranchVo();
                    BeanUtils.copyProperties(child, processBranchVo);
                    Integer level = child.getLevel() - 1;
                    if (level == 0) {
                        processBranchVo.setIndex(index + "");
                    } else {
                        processBranchVo.setIndex(processBranch.getIndex() + "." + index);
                    }
                    index ++;
                    children.add(processBranchVo);
                }
            }
            processBranch.setChildren(children);
            findChildren(children, processBranches, conditionFlag);
        }
    }

    @Resource
    private IInterfaceInfoService  interfaceInfoService;

    @Resource
    private IVirtualUsageGroupService virtualUsageGroupService;

    /**
     * 获取接口下虚拟用况组列表：
     * return: [{
     *     "id":1,
     *     "groupName":"组1"
     * }]
     * @author chenxiaoyu
     * @date 2022/4/22 下午11:39
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/queryVirtualUsageGroup")
    @ResponseBody
    public AjaxResult queryVirtualUsageGroup(Long interfaceId) {
        VirtualUsageGroup virtualUsageGroup = new VirtualUsageGroup();
        virtualUsageGroup.setInterfaceId(interfaceId);
        List<VirtualUsageGroup> virtualUsageGroups = this.virtualUsageGroupService.selectVirtualUsageGroupList(virtualUsageGroup);
        return AjaxResult.success(virtualUsageGroups);
    }

    /**
     * 保存/更新虚拟用况（虚拟用况组）分支流程
     * eq:
     * {
     *     "processBranchId":1, // 更新时 必填
     *     "interfaceId" : 1, //bff或者dubbo接口id(必填)
     *     "preProcessBranchId":11, //追加节点的流程分支id
     *     "parentProcessBranchId": 1, // 父类id(没有就传null)
     *     "entityId":1, // 虚拟用况id
     *     "processEndFlag":"Y",  //是否设置流程结束 Y-是  N-否
     *     "type":"1", // 1-虚拟用况，4-虚拟用况组
     *     "sortNum":0, // 指定插入顺序
     *     "aliasName":"xxxx" //虚拟用况别名
     * }
     * @author chenxiaoyu
     * @date 2022/4/2 下午8:11
     * @param processBranch
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/saveVirtualUsage")
    @ResponseBody
    public AjaxResult saveVirtualUsage(@RequestBody ProcessBranch processBranch) {
        this.processBranchService.saveOrUpdateVirtualUsage(processBranch);
        return success();
    }

    /**
     * 保存正常结束分支流程
     * eq:
     * {
     *     "interfaceId" : 1, //bff或者dubbo接口id(必填)
     *     "parentProcessBranchId": 1, // 父类id(没有就传null)
     * }
     * @author chenxiaoyu
     * @date 2022/4/2 下午9:52
     * @param processBranch
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/saveEnd")
    @ResponseBody
    public AjaxResult saveEnd(@RequestBody ProcessBranch processBranch) {
        this.processBranchService.saveEnd(processBranch);
        return success();
    }

    /**
     * 保存/更新条件分支流程
     * eq:
     * {
     *     "processBranchId":1, // 更新时 必填
     *     "interfaceId" : 1, //bff或者dubbo接口id(必填)
     *     "parentProcessBranchId": 1, // 父类id(没有就传null)
     *     "processEndFlag":"Y",  //是否设置流程结束 Y-是  N-否
     *     "preProcessBranchId":11, //追加节点的流程分支id
     *     "sortNum":4,// 追加节点的sortNum + 1
     *     "processBranchCondition":{ // 流程条件分支信息
     *         type:'if',//类型（if，else if，else）
     *         joinCondition: and, // 关联条件（AND，OR）
     *         branchConditionDesc: '111',分支条件描述
     *         processBranchConditionDetailList:[{
     *             branchConditionDetailDictCode: '1',//分支条件模式字典数据ID (1-字段 2-数据集条数)
     *             branchConditionGroupId:'1',//分支条件字典数据分组(1-组1 .. n-组n)
     *             objectCategory:'1',// 对象分类（1-字段，2-数据集，3-出参）
     *             objectType: '1', // 对象类型（1-字段，2-数据集，3-数据集字段）
     *             objectId:111,// 字段ID/数据集ID/数据集字段id
     *             paramsId:1,//数据集id，当对象类型为3 时，必填
     *             interfaceId:1,// 接口id
     *             interfaceName:"",//接口名称
     *             objectProcessBranchId:1,//来源流程id
     *             objectName: "名称", // 对象名称(字段或者数据集)
     *             objectFieldName: "对象字段名称", // 对象字段名称(数据集字段)
     *             judgeConditionDictCode:'1', //判断条件字典
     *             "contractDictCategory": 2, // 约定字典分类 (约定值三级联动)
     *             "contractDictType": 2, // 约定字典 (约定值三级联动)
     *             contractValue:'admin' //约定值
     *             dictName:'是', // 字典名称
     *             joinCondition: 'and', //分组中 条件 and / or
     *             sortNum:1 //序号
     *         }]
     *     }
     *
     * }
     * @author chenxiaoyu
     * @date 2022/4/2 下午9:52
     * @param processBranch
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/saveCondition")
    @ResponseBody
    public AjaxResult saveCondition(@RequestBody ProcessBranch processBranch) {
        this.processBranchService.saveOrUpdateCondition(processBranch);
        return success();
    }

    /**
     * 保存跳出循环分支流程
     * eq:
     * {
     *     "interfaceId" : 1, //bff或者dubbo接口id(必填)
     *     "parentProcessBranchId": 1, // 父类id(没有就传null)
     * }
     * @author chenxiaoyu
     * @date 2022/4/2 下午9:52
     * @param processBranch
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/saveBreakEnd")
    @ResponseBody
    public AjaxResult saveBreakEnd(@RequestBody ProcessBranch processBranch) {
        this.processBranchService.saveBreakEnd(processBranch);
        return success();
    }

    /**
     * 保存跳出循环分支流程
     * eq:
     * {
     *     "interfaceId" : 1, //bff或者dubbo接口id(必填)
     *     "parentProcessBranchId": 1, // 父类id(没有就传null)
     * }
     * @author chenxiaoyu
     * @date 2022/4/2 下午9:52
     * @param processBranch
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/saveContinueEnd")
    @ResponseBody
    public AjaxResult saveContinueEnd(@RequestBody ProcessBranch processBranch) {
        this.processBranchService.saveContinueEnd(processBranch);
        return success();
    }


    /**
     * 保存异常结束分支流程
     * eq:
     * {
     *     "interfaceId" : 1, //bff或者dubbo接口id(必填)
     *     "parentProcessBranchId": 1, // 父类id(没有就传null)
     * }
     * @author chenxiaoyu
     * @date 2022/4/2 下午9:52
     * @param processBranch
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/saveExceptionEnd")
    @ResponseBody
    public AjaxResult saveExceptionEnd(@RequestBody ProcessBranch processBranch) {
        this.processBranchService.saveExceptionEnd(processBranch);
        return success();
    }

    /**
     * 获取接口列表
     * @param processBranchId 流程id
     * @param interfaceId 接口id
     * @return
     */
    @GetMapping("/queryInterfaceList")
    @ResponseBody
    public AjaxResult queryInterfaceList(Long processBranchId, Long interfaceId) {
        ProcessBranch processBranch = null;
        if (processBranchId != null) {
            processBranch = this.processBranchService.selectProcessBranchByProcessBranchId(processBranchId);
            interfaceId = processBranch.getInterfaceId();
        }
        List<ParamObject> paramObjectList = new ArrayList<>();
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(interfaceId);
        String interfaceType = interfaceInfo.getFbType();
        if (interfaceType.equals("3")) {
            interfaceType = "4";
        }
        ParamObject paramObject = new ParamObject();
        paramObject.setInterfaceName(interfaceInfo.getInterfaceName());
        paramObject.setInterfaceId(interfaceId);
        paramObject.setInterfaceType(interfaceType);
        paramObject.setObjectProcessBranchId(0l);
        paramObjectList.add(paramObject);
        List<VirtualUsage> virtualUsages = null;
        if (processBranchId != null) {
            //查询虚拟用况接口出参
            virtualUsages = this.processBranchService.selectVirtualUsageIdsByProcessBranch(processBranch);
            if (processBranch.getType().equals("1")) {
                VirtualUsage virtualUsage = new VirtualUsage();
                virtualUsage.setId(processBranch.getEntityId());
                List<VirtualUsage> virtualUsageList = this.virtualUsageService.selectVirtualUsages(virtualUsage);
                if (virtualUsageList.size() > 0) {
                    VirtualUsage e = virtualUsageList.get(0);
                    e.setVirtualUsageType("1");
                    e.setProcessBranchId(processBranchId);
                    if (StringUtils.isNotEmpty(processBranch.getAliasName())) {
                        e.setSlaveInterfaceName(processBranch.getAliasName());
                    }
                    virtualUsages.add(e);
                }
            } else if (processBranch.getType().equals("4")) {
                VirtualUsageGroup virtualUsageGroup = this.virtualUsageGroupService.selectVirtualUsageGroupById(processBranch.getEntityId());
                VirtualUsage e = new VirtualUsage();
                e.setId(processBranch.getEntityId());
                e.setSlaveInterfaceName(virtualUsageGroup.getGroupName());
                e.setVirtualUsageType("2");
                virtualUsages.add(e);
            }
        } else {
            virtualUsages = this.processBranchService.selectVirtualUsageIdsByLevel(1l, interfaceId);
        }
        if (CollectionUtil.isNotEmpty(virtualUsages)) {
            for (VirtualUsage virtualUsage : virtualUsages) {
                ParamObject object = new ParamObject();
                object.setInterfaceName(virtualUsage.getSlaveInterfaceName());
                object.setInterfaceId(virtualUsage.getId());
                if (virtualUsage.getVirtualUsageType().equals("1")) {
                    object.setInterfaceType("3");
                } else {
                    object.setInterfaceType("5");
                }
                object.setObjectProcessBranchId(virtualUsage.getProcessBranchId());
                paramObjectList.add(object);
            }
        }
        return AjaxResult.success(paramObjectList);
    }

    /**
     * 根据接口id，查询字段/数据集集合
     * @author chenxiaoyu
     * @date 2022/4/3 上午12:27
     * @param interfaceId 接口id
     * @param interfaceType 接口类型
     * @param type 1-字段 2-数据集
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/queryFieldList")
    @ResponseBody
    public AjaxResult queryParamList(Long interfaceId, String interfaceType, String type) {
        if ("3".equals(interfaceType)) {
            List<ParamObject> paramObjectList = this.processBranchService.paramObjectList(
                    interfaceType, Arrays.asList(interfaceId), "2", type);
            return AjaxResult.success(paramObjectList);
        } else {
            List<ParamObject> paramObjectList = this.processBranchService.paramObjectList(
                    interfaceType, Arrays.asList(interfaceId), "1", type);
            return AjaxResult.success(paramObjectList);
        }
    }

    /**
     * 根据数据集id，查询数据集字段集合
     * @author chenxiaoyu
     * @date 2022/4/3 上午12:27
     * @param id 数据集id
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/queryParamFieldList")
    @ResponseBody
    public AjaxResult queryParamFieldList(Long id) {
        List<ParamObject> paramObjectList = this.processBranchService.queryParamFieldList(id);
        return AjaxResult.success(paramObjectList);
    }

    /**
     * 删除流程分支
     * @author chenxiaoyu
     * @date 2022/4/3 下午4:20
     * @param processBranchId
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long processBranchId) {
        return toAjax(processBranchService.delete(processBranchId));
    }

    @Resource
    private DictService dictService;

    /**
     * 查询 判断条件字典
     * @author chenxiaoyu
     * @date 2022/4/3 下午4:19
     * @param
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/conditionDictList")
    @ResponseBody
    public AjaxResult conditionDictList() {
        return AjaxResult.success(dictService.getType("JUDGE_CONDITION"));
    }

    /**
     * 虚拟用况接口详情
     * @param processBranchId 流程分支id
     * @return
     */
    @GetMapping("/bff_usage_params")
    public String bff_usage_params(Long processBranchId, ModelMap mmap) {
        ProcessBranch processBranch = this.processBranchService.selectProcessBranchByProcessBranchId(processBranchId);
        VirtualUsage virtualUsage = new VirtualUsage();
        virtualUsage.setId(processBranch.getEntityId());
        List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsages(virtualUsage);
        mmap.put("virtualUsage", virtualUsages.get(0));
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(virtualUsages.get(0).getRelationInterfaceId());
        interfaceInfo.setIsBranch("N");
        mmap.put("interfaceInfo", interfaceInfo);
        mmap.put("processBranchId", processBranchId);
        return "processBranch/bff_usage_params";
    }

    @Resource
    private IParamsFieldRelationService paramsFieldRelationService;
    @Resource
    private IVirtualUsageParamSourceService virtualUsageParamSourceService;
    @Resource
    private IInterfaceParamsFieldRelationService interfaceParamsFieldRelationService;



    @GetMapping("/fieldSourceChoose")
    public String fieldSourceChoose(VirtualUsageParamSourcePo virtualUsageParamSourcePo, ModelMap modelMap) {
        modelMap.put("virtualUsageParamSourcePo", virtualUsageParamSourcePo);
        List<InterfaceObjectParam> interfaceObjectParamList = new ArrayList<>();
        int index = 1;
        VirtualUsageParamSource paramSource = new VirtualUsageParamSource();
        paramSource.setObjectId(virtualUsageParamSourcePo.getInterfaceFieldId());
        Long processBranchId = virtualUsageParamSourcePo.getProcessBranchId();
        paramSource.setProcessBranchId(processBranchId);
        if (virtualUsageParamSourcePo.getInterfaceFieldType().equals("3")) {
            modelMap.put("type", "2");
            paramSource.setType("2");
            // 查询字段对应的数据集
            ParamsFieldRelation paramsFieldRelation = this.paramsFieldRelationService.selectParamsFieldRelationById(virtualUsageParamSourcePo.getInterfaceFieldId());
            VirtualUsageParamSource virtualUsageParamSource = new VirtualUsageParamSource();
            virtualUsageParamSource.setObjectId(paramsFieldRelation.getInterfaceDataListId());
            virtualUsageParamSource.setType("1");
            virtualUsageParamSource.setProcessBranchId(processBranchId);
            List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList(virtualUsageParamSource);
            if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
                // 查询字段所对应数据集的来源数据集下字段集合
                Long sourceObjectId = virtualUsageParamSources.get(0).getSourceobjectId();
                // 查询来源数据集 接口名称
                String interfaceName = null;
                InterfaceParamsFieldRelation interfaceParamsFieldRelation = this.interfaceParamsFieldRelationService.selectInterfaceParamsFieldRelationById(sourceObjectId);
                if (interfaceParamsFieldRelation.getType().equals("3")) {
                    // 虚拟用况接口
                    VirtualUsage virtualUsage = new VirtualUsage();
                    virtualUsage.setId(interfaceParamsFieldRelation.getInterfaceId());
                    List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsages(virtualUsage);
                    interfaceName = virtualUsages.get(0).getSlaveInterfaceName();
                } else if (interfaceParamsFieldRelation.getType().equals("5")) {
                    // 虚拟用况组接口
                    VirtualUsageGroup virtualUsageGroup = this.virtualUsageGroupService.selectVirtualUsageGroupById(interfaceParamsFieldRelation.getInterfaceId());
                    interfaceName = virtualUsageGroup.getGroupName();
                } else {
                    InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(interfaceParamsFieldRelation.getInterfaceId());
                    interfaceName = interfaceInfo.getInterfaceName();
                }
                List<ObjectParam> objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getInterfaceId(),
                        virtualUsageParamSourcePo.getFbType(),
                        null, virtualUsageParamSourcePo.getInterfaceFieldType(), sourceObjectId);
                for (ObjectParam objectParam : objectParamList) {
                    objectParam.setProcessBranchId(virtualUsageParamSources.get(0).getSourceProcessBranchId());
                }
                interfaceObjectParamList.add(new InterfaceObjectParam(index, interfaceName, objectParamList));
            }
        } else {
            modelMap.put("type", "1");
            paramSource.setType("1");
            InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(virtualUsageParamSourcePo.getInterfaceId());
            // 查询接口（bff/dubbo）入参
            List<ObjectParam> objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getInterfaceId(),
                    virtualUsageParamSourcePo.getFbType(),
                    "1", virtualUsageParamSourcePo.getInterfaceFieldType(), null);
            for (ObjectParam objectParam : objectParamList) {
                objectParam.setProcessBranchId(0l);
            }
            interfaceObjectParamList.add(new InterfaceObjectParam(index ++, interfaceInfo.getInterfaceName(), objectParamList));
            // 查询虚拟用况
            //查询虚拟用况接口出参
            ProcessBranch processBranch = this.processBranchService.selectProcessBranchByProcessBranchId(processBranchId);
            List<VirtualUsage> virtualUsageIds = this.processBranchService.selectVirtualUsageIdsByProcessBranch(processBranch);
            if (CollectionUtil.isNotEmpty(virtualUsageIds)) {
                for (VirtualUsage usage : virtualUsageIds) {
                    if (usage.getVirtualUsageType().equals("1")) {
                        //  虚拟用况
                        objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                                "3",
                                "2", virtualUsageParamSourcePo.getInterfaceFieldType(), null);
                    } else {
                        //  虚拟用况组
                        objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                                "5",
                                "2", virtualUsageParamSourcePo.getInterfaceFieldType(), null);
                    }
                    for (ObjectParam objectParam : objectParamList) {
                        objectParam.setProcessBranchId(usage.getProcessBranchId());
                    }
                    interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), objectParamList));
                }
            }
            if (virtualUsageParamSourcePo.getVirtualUsageType().equals("1")) {
                objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getVirtualUsageId(),
                        "3",
                        "1", virtualUsageParamSourcePo.getInterfaceFieldType(), null);
            } else {
                objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getVirtualUsageId(),
                        "5",
                        "1", virtualUsageParamSourcePo.getInterfaceFieldType(), null);
            }
            for (ObjectParam objectParam : objectParamList) {
                objectParam.setProcessBranchId(processBranchId);
            }
            interfaceObjectParamList.add(new InterfaceObjectParam(index ++, virtualUsageParamSourcePo.getVirtualUsageName(), objectParamList));
        }
        modelMap.put("interfaceObjectParamList", interfaceObjectParamList);
        // 查询参数的来源
        List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList(paramSource);

        if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
            modelMap.put("sourceId", virtualUsageParamSources.get(0).getSourceobjectId());
            modelMap.put("sourceProcessBranchId", virtualUsageParamSources.get(0).getSourceProcessBranchId());
            modelMap.put("id", virtualUsageParamSources.get(0).getVirtualUsageParamSourceId());
        } else {
            modelMap.put("sourceId", "");
            modelMap.put("sourceProcessBranchId", "");
            modelMap.put("id", "");
        }

        return "processBranch/field_source_choose";
    }

    /**
     * TODO
     * @author chenxiaoyu
     * @date 2022/4/9 上午11:12
     * @param virtualUsageParamSourcePo
     * @param modelMap
     * @return java.lang.String
     */
    @GetMapping("/fieldSourceChoose2")
    public String fieldSourceChoose2(VirtualUsageParamSourcePo virtualUsageParamSourcePo, ModelMap modelMap) {
        modelMap.put("virtualUsageParamSourcePo", virtualUsageParamSourcePo);
        List<InterfaceObjectParam> interfaceObjectParamList = new ArrayList<>();
        int index = 1;
        VirtualUsageParamSource paramSource = new VirtualUsageParamSource();
        paramSource.setObjectId(virtualUsageParamSourcePo.getInterfaceFieldId());
        Long processBranchId = virtualUsageParamSourcePo.getProcessBranchId();
        paramSource.setProcessBranchId(processBranchId);

        modelMap.put("type", "1");
        paramSource.setType("2");
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(virtualUsageParamSourcePo.getInterfaceId());
        // 查询接口（bff/dubbo）入参
        String interfaceFieldType = "1";
        List<ObjectParam> objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getInterfaceId(),
                virtualUsageParamSourcePo.getFbType(),
                "1", interfaceFieldType, null);
        for (ObjectParam objectParam : objectParamList) {
            objectParam.setProcessBranchId(0l);
        }
        interfaceObjectParamList.add(new InterfaceObjectParam(index ++, interfaceInfo.getInterfaceName(), objectParamList));
        // 查询虚拟用况
        //查询虚拟用况接口出参
        ProcessBranch processBranch = this.processBranchService.selectProcessBranchByProcessBranchId(processBranchId);
        List<VirtualUsage> virtualUsageIds = this.processBranchService.selectVirtualUsageIdsByProcessBranch(processBranch);
        if (CollectionUtil.isNotEmpty(virtualUsageIds)) {
            for (VirtualUsage usage : virtualUsageIds) {
                if (usage.getVirtualUsageType().equals("1")) {
                    objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                            "3",
                            "2", interfaceFieldType, null);
                } else {
                    objectParamList = this.virtualUsageParamSourceService.objectParamList(usage.getId(),
                            "5",
                            "2", interfaceFieldType, null);
                }
                for (ObjectParam objectParam : objectParamList) {
                    objectParam.setProcessBranchId(usage.getProcessBranchId());
                }
                interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), objectParamList));
            }
        }
        if (virtualUsageParamSourcePo.getVirtualUsageType().equals("1")) {
            objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getVirtualUsageId(),
                    "3",
                    "1", interfaceFieldType, null);
        } else {
            objectParamList = this.virtualUsageParamSourceService.objectParamList(virtualUsageParamSourcePo.getVirtualUsageId(),
                    "5",
                    "1", interfaceFieldType, null);
        }
        for (ObjectParam objectParam : objectParamList) {
            objectParam.setProcessBranchId(processBranchId);
        }
        interfaceObjectParamList.add(new InterfaceObjectParam(index ++, virtualUsageParamSourcePo.getVirtualUsageName(), objectParamList));

        modelMap.put("interfaceObjectParamList", interfaceObjectParamList);
        // 查询参数的来源
        List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList(paramSource);

        if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
            modelMap.put("sourceId", virtualUsageParamSources.get(0).getSourceobjectId());
            modelMap.put("sourceProcessBranchId", virtualUsageParamSources.get(0).getSourceProcessBranchId());
            modelMap.put("id", virtualUsageParamSources.get(0).getVirtualUsageParamSourceId());
        } else {
            modelMap.put("sourceId", "");
            modelMap.put("sourceProcessBranchId", "");
            modelMap.put("id", "");
        }

        return "processBranch/field_source_choose2";
    }

    @Resource
    private IProcessBranchConditionDetailService processBranchConditionDetailService;

    /**
     * 查询流程分支详情
     * @author chenxiaoyu
     * @date 2022/4/4 下午3:25
     * @param processBranchId
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/getProcessBranchDetail")
    @ResponseBody
    public AjaxResult getProcessBranchDetail(Long processBranchId) {
        ProcessBranch processBranch = this.processBranchService.selectProcessBranchByProcessBranchId(processBranchId);
        if (processBranch.getType().equals("2")) {
            ProcessBranchCondition processBranchCondition = this.processBranchConditionService.selectProcessBranchConditionByBranchConditionId(processBranch.getEntityId());
            ProcessBranchConditionDetail processBranchConditionDetail = new ProcessBranchConditionDetail();
            processBranchConditionDetail.setBranchConditionId(processBranchCondition.getBranchConditionId());
            List<ProcessBranchConditionDetail> details = this.processBranchConditionDetailService.selectProcessBranchConditionDetailList(processBranchConditionDetail);
            processBranchCondition.setProcessBranchConditionDetailList(details);
            processBranch.setProcessBranchCondition(processBranchCondition);
            processBranch.setProcessBranchDescription(processBranchCondition.getBranchConditionDesc());
        } else if (processBranch.getType().equals("1")){
            VirtualUsage virtualUsage = new VirtualUsage();
            virtualUsage.setId(processBranch.getEntityId());
            List<VirtualUsage> virtualUsages = this.virtualUsageService.selectVirtualUsages(virtualUsage);
            processBranch.setProcessBranchDescription(virtualUsages.get(0).getDescription());
            processBranch.setProcessBranchName(virtualUsages.get(0).getSlaveInterfaceName());
            List<FieldSourceVo> fieldSourceVoList = this.virtualUsageService.selectFieldSource(processBranch.getEntityId(), processBranchId);
            processBranch.setFieldSourceVoList(fieldSourceVoList);
        } else if (processBranch.getType().equals("4")){
            VirtualUsageGroup virtualUsageGroup = this.virtualUsageGroupService.selectVirtualUsageGroupById(processBranch.getEntityId());
            processBranch.setProcessBranchName(virtualUsageGroup.getGroupName());
            processBranch.setProcessBranchDescription(virtualUsageGroup.getGroupDescription());
        } else {
            ProcessBranchEnd end = this.processBranchEndService.selectProcessBranchEnd2(processBranch.getEntityId());
            processBranch.setProcessBranchEnd(end);
        }
        return AjaxResult.success(processBranch);
    }

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 查询字段枚举
     * @author chenxiaoyu
     * @date 2022/4/5 下午11:21
     * @param dictId
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/queryEnum")
    @ResponseBody
    public AjaxResult queryEnum(Long dictId) {
        SysDictType sysDictType = this.sysDictTypeService.selectDictTypeById(dictId);
        List<SysDictData> sysDictDatas = new ArrayList<>();
        if (sysDictType != null) {
            SysDictData dictData = new SysDictData();
            dictData.setDictType(sysDictType.getDictType());
            sysDictDatas = this.sysDictDataService.selectDictDataList(dictData);
        }
        return AjaxResult.success(sysDictDatas);
    }


    /**
     * 字段来源选择(字段、数据集、数据集字段全部展示)
     * @author chenxiaoyu
     * @date 2022/3/31 下午9:24
     * @param virtualUsageParamSourcePo 参数
     * @param modelMap 视图
     * @return java.lang.String
     */
    @GetMapping("/fieldSourceAllSelect")
    public String fieldSourceAllSelect(VirtualUsageParamSourcePo virtualUsageParamSourcePo, ModelMap modelMap) {
        modelMap.put("virtualUsageParamSourcePo", virtualUsageParamSourcePo);
        List<InterfaceObjectParam> interfaceObjectParamList = new ArrayList<>();

        VirtualUsageParamSource paramSource = new VirtualUsageParamSource();
        paramSource.setObjectId(virtualUsageParamSourcePo.getInterfaceFieldId());
        Long processBranchId = virtualUsageParamSourcePo.getProcessBranchId();
        paramSource.setProcessBranchId(processBranchId);
        if (virtualUsageParamSourcePo.getInterfaceFieldType().equals("3")) {
            modelMap.put("type", "2");
            paramSource.setType("2");
        } else {
            modelMap.put("type", "1");
            paramSource.setType("1");
        }
        // 查询参数的来源
        List<VirtualUsageParamSource> virtualUsageParamSources = this.virtualUsageParamSourceService.selectVirtualUsageParamSourceList2(paramSource);
        if (CollectionUtil.isNotEmpty(virtualUsageParamSources)) {
            modelMap.put("sourceId", virtualUsageParamSources.get(0).getSourceobjectId());
            modelMap.put("sourceType", virtualUsageParamSources.get(0).getSourceType());
            modelMap.put("sourceProcessBranchId", virtualUsageParamSources.get(0).getSourceProcessBranchId());
            modelMap.put("id", virtualUsageParamSources.get(0).getVirtualUsageParamSourceId());
        } else {
            modelMap.put("sourceId", "");
            modelMap.put("sourceType", "");
            modelMap.put("sourceProcessBranchId", "");
            modelMap.put("id", "");
        }
        int index = 1;
        InterfaceInfo interfaceInfo = this.interfaceInfoService.selectInterfaceInfoById(virtualUsageParamSourcePo.getInterfaceId());
        // 查询接口（bff/dubbo）入参
        List<InterfaceParam> interfaceParams = this.interfaceInfoService.paramList(virtualUsageParamSourcePo.getFbType(), virtualUsageParamSourcePo.getInterfaceId(), "1");
        List<ObjectParam> objectParamList = VirtualUsageParamSourceController.convertObjectParamList(interfaceParams);
        for (ObjectParam objectParam : objectParamList) {
            objectParam.setProcessBranchId(0l);
        }
        interfaceObjectParamList.add(new InterfaceObjectParam(index ++, interfaceInfo.getInterfaceName(), objectParamList));
        // 查询虚拟用况
        //查询虚拟用况接口出参
        ProcessBranch processBranch = this.processBranchService.selectProcessBranchByProcessBranchId(processBranchId);
        List<VirtualUsage> virtualUsageIds = this.processBranchService.selectVirtualUsageIdsByProcessBranch(processBranch);
        if (CollectionUtil.isNotEmpty(virtualUsageIds)) {
            for (VirtualUsage usage : virtualUsageIds) {
                if ("Y".equals(usage.getIsMaster())) {
                    usage.setSlaveInterfaceName(usage.getSlaveInterfaceName() + " （主）");
                }
                if (usage.getVirtualUsageType().equals("1")) {
                    //  虚拟用况
                    interfaceParams = this.interfaceInfoService.paramList("3", usage.getId(), "2");
                    objectParamList = VirtualUsageParamSourceController.convertObjectParamList(interfaceParams);
                } else {
                    //  虚拟用况组
                    interfaceParams = this.interfaceInfoService.paramList("5", usage.getId(), "2");
                    objectParamList = VirtualUsageParamSourceController.convertObjectParamList(interfaceParams);
                }
                for (ObjectParam objectParam : objectParamList) {
                    objectParam.setProcessBranchId(usage.getProcessBranchId());
                }
                interfaceObjectParamList.add(new InterfaceObjectParam(index ++, usage.getSlaveInterfaceName(), objectParamList));
            }
        }
        if (virtualUsageParamSourcePo.getVirtualUsageType().equals("1")) {
            VirtualUsage virtualUsage = this.virtualUsageService.selectVirtualUsageById(virtualUsageParamSourcePo.getVirtualUsageId());
            if (virtualUsage != null && "Y".equals(virtualUsage.getIsMaster())) {
                virtualUsageParamSourcePo.setVirtualUsageName(virtualUsageParamSourcePo.getVirtualUsageName() + " （主）");
            }
            interfaceParams = this.interfaceInfoService.paramList("3", virtualUsageParamSourcePo.getVirtualUsageId(), "1");
            objectParamList = VirtualUsageParamSourceController.convertObjectParamList(interfaceParams);
        } else {
            VirtualUsageGroup virtualUsageGroup = this.virtualUsageGroupService.selectVirtualUsageGroupById(virtualUsageParamSourcePo.getVirtualUsageId());
            if (virtualUsageGroup != null && "Y".equals(virtualUsageGroup.getIsMaster())) {
                virtualUsageParamSourcePo.setVirtualUsageName(virtualUsageParamSourcePo.getVirtualUsageName() + " （主）");
            }
            interfaceParams = this.interfaceInfoService.paramList("5", virtualUsageParamSourcePo.getVirtualUsageId(), "1");
            objectParamList = VirtualUsageParamSourceController.convertObjectParamList(interfaceParams);
        }
        for (ObjectParam objectParam : objectParamList) {
            objectParam.setProcessBranchId(processBranchId);
        }
        interfaceObjectParamList.add(new InterfaceObjectParam(index ++, virtualUsageParamSourcePo.getVirtualUsageName(), objectParamList));

        modelMap.put("interfaceObjectParamList", interfaceObjectParamList);
        return "processBranch/field_source_choose";
    }
}
